<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminPrice as price_model;

class AdminPrice extends AdminRest{
    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new price_model();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:价格列表
     */
    public function priceList(){
        $input = $this->_param;
        $data  = $this->model->priceList(['uniacid'=>$this->_uniacid],$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加价格
     */
    public function priceAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;

        $this->checkData($input);
        $res = $this->model->priceAdd($input);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显价格
     */
    public function priceUpdate(){
        $input = $this->_input;
        $data  = $this->model->priceInfo(['id'=>$input['id']]);
        if(count($input)>1){
            $this->checkData($input);
            $data = $this->model->priceUpdate(['id'=>$input['id']],$input);
        }
        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-10-21 10:44
     * @return \think\Response
     * descption:上下架|删除
     */

    public function changeStatus(){
        $input = $this->_input;
        $data = $this->model->priceUpdate(['id'=>$input['id']],$input);
        return $this->success($data);

    }


    /**
     * User: chenniang
     * Date: 2019-10-14 15:06
     * @param $input
     * @return bool
     * descption:检查是否有重复数据
     */
    public function checkData($input){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['min'    ,'=',$input['min']];
        $dis[] = ['max'    ,'=',$input['max']];
        $dis[] = ['status' ,'>',-1];
        if(!empty($input['id'])){
            $dis[] = ['id' ,'<>',$input['id']];
        }
        $data  = $this->model->priceInfo($dis);
        if(!empty($data)){
            $this->errorMsg('已有该数据,不可重复添加编辑');
        }
        return true;
    }







}
